package Blog0;

import java.util.Arrays;


class Student implements Comparable <Student> {  // 实现 Comparable 接口
    public String name;
    public int age;
    public int score;

    public Student(String name, int age,int score) {
        this.name = name;
        this.age = age;
        this.score = score;

    }

    @Override // 重写，注解：提示错误，人和编译器都可以读懂的注释
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override // 重写 该 Comparable接口中的抽象类 compareTo
    public int compareTo(Student o) {
        if(this.score > o.score) {  // 根据类中的成句比较，排序
            return 1;
        } else if(this.score == o.score) {
            return 0;
        } else {
            return -1;
        }
    }
}


class Money implements Cloneable {
    public int money = 10;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // 注意这里的 返回类型是 Object ,
    }
}


class Persons implements Cloneable {

    public int age = 18;

    Money moneys = new Money();


    @Override // 对clone 的重写，
    protected Object clone() throws CloneNotSupportedException {

        // 克隆 person
        Persons p = (Persons)super.clone(); // 将 Persons 中克隆(clone())的返回类类型，Object类类型强转为 Persons类类型
        // 克隆当前的 Money 对象
        p.moneys = (Money) this.moneys.clone(); // 将 Money中的克隆(clone())的返回类类型，Object类类型强转为 Money 类型

        return p;
    }

}

public class Blog07 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Persons person = new Persons();
        Persons person2 = (Persons)person.clone(); // 将 Object类类型 强转为 Persons类类型，克隆，深拷贝

        System.out.println("person.moneys.money: "+person.moneys.money);
        System.out.println("person2.moneys.money: "+person2.moneys.money);

        System.out.println("*******修改后******");

        person.moneys.money = 1000_000_000;
        System.out.println("person.moneys.money: "+person.moneys.money);
        System.out.println("person2.moneys.money: "+person2.moneys.money);

        System.out.println("*******************");

        System.out.println("person.moneys 地址: "+person.moneys);
        System.out.println("person2.moneys 地址: "+person2.moneys);
    }


    public static void main5(String[] args) throws CloneNotSupportedException {
        Persons person = new Persons();
        Persons person2 = (Persons)person.clone(); // 将 Object类类型 强转为 Persons类类型，克隆，深拷贝
        System.out.println("person.age: "+person.age);
        System.out.println("person2.age: "+person2.age);
        System.out.println("person.moneys.money: "+person.moneys.money);
        System.out.println("person2.moneys.money: "+person2.moneys.money);

        System.out.println("*******修改后******");

        person2.age = 99;
        System.out.println(person.age);
        System.out.println(person2.age);

        System.out.println("*******************");

        System.out.println(person);
        System.out.println(person2);
    }



    public static void main4(String[] args) {
        Student student = new Student("小明",17,89);
        Student student1 = new Student("小红",18,90);
        Student student2 = new Student("小华",18,100);

   /*     if(student.compareTo(student1) < 0) {
            System.out.println("sudent < sudent1");
        }
*/

        Student[] students = new Student[] {  // 创建该类 Student类的数组
                student,
                student1,
                student2
        };

        Arrays.sort(students);

        System.out.println(Arrays.toString(students));


    }


    public static void main3(String[] args) {
        Student student = new Student("小明",17,89);
        Student student1 = new Student("小红",18,90);
        Student student2 = new Student("小华",18,100);

        Student[] students = new Student[] {  // 创建该类 Student类的数组
                student,
                student1,
                student2
        };


        Arrays.sort(students);  // 试试 使用Arrays.sort()对 自定义的类的数组排序

        System.out.println(Arrays.toString(students)); // 一字符串的形式打印数组
    }


    public static void main2(String[] args) {
        Student[] studens = new Student[] {
                new Student("小明",17,89),
                new Student("小华",18,90)
        };
    }


    public static void main1(String[] args) {
        int[] arr = new int[]{100,250,36,46,55,66};

        Arrays.sort(arr); // 升序排序数组

        System.out.println(Arrays.toString(arr));

    }
}
